from dataclasses import dataclass
from typing import BinaryIO, Callable, List
import os
from .interface import Decoder


@dataclass
class DecoderParams:
    reader: BinaryIO
    extension: str
    file_path: str = ""
    logger: object = None

NewDecoderFunc = Callable[[DecoderParams], Decoder]

@dataclass
class DecoderFactory:
    noop: bool
    suffix: str
    create: NewDecoderFunc

decoder_registry: List[DecoderFactory] = []

def get_registery() -> List[DecoderFactory]:
    return decoder_registry

def register_decoder(ext: str, noop: bool, dispatch_func: NewDecoderFunc) -> None:
    decoder_registry.append(DecoderFactory(noop=noop, suffix=f".{ext.lstrip('.')}", create=dispatch_func))

def get_decoders(filename: str, skip_noop: bool) -> List[DecoderFactory]:
    result = []
    name = os.path.basename(filename).lower()
    for dec in decoder_registry:
        if not name.endswith(dec.suffix):
            continue
        if skip_noop and dec.noop:
            continue
        result.append(dec)
    return result
